
    //
//  XODetailGoodsViewCell.m
//  didigo
//
//  Created by JS_Coder on 2019/3/17.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XODetailGoodsViewCell.h"
#import "EJSGoodsModel.h"
#import "EJSSelectGoodsSpecView.h"
#import "EJSGoodsAddCartRequest.h"
#import "XOShopCartViewModel.h"
//#import "EJSGoodsSkuModel.h"

@interface XODetailGoodsViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *goodImageView;
@property (weak, nonatomic) IBOutlet UILabel *goodNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodAddedCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodUnitLabel;
@property (weak, nonatomic) IBOutlet UIWebView *goodDescriptionWebView;
@property (weak, nonatomic) IBOutlet UILabel *originPrice;
@property (weak, nonatomic) IBOutlet UILabel *discountLine;
@property (strong, nonatomic) EJSGoodsSkuModel *selectSkuModel;
@end

@implementation XODetailGoodsViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setGoodModel:(EJSGoodsModel *)goodModel {
    _goodModel = goodModel;
    [self setData];
}

- (void)setData {
    NSString *imgStyle=@"<style type=\"text/css\">img{max-width:100%;height:auto;} </style>";
    [self.goodImageView setImageWithUrl:self.goodModel.cover];
    self.goodNameLabel.text = self.goodModel.spuName;
    self.goodPriceLabel.text = [NSString stringWithFormat:@"%@ €",self.goodModel.price];
    self.originPrice.text = self.goodModel.originPrice;

    if (self.goodModel.price.doubleValue == self.goodModel.originPrice.doubleValue || self.goodModel.originPrice.doubleValue == 0.00) {
        [self.originPrice setHidden:YES];
        [self.discountLine setHidden:YES];
    }else {
        [self.originPrice setHidden:NO];
        [self.discountLine setHidden:NO];
    }
    if (self.goodModel.addCartNum.integerValue / self.goodModel.minInCart.integerValue < 1) {
        [self.goodAddedCountLabel setHidden:YES];
    }else {
        [self.goodAddedCountLabel setHidden:NO];
        self.goodAddedCountLabel.text = [NSString stringWithFormat:@"%ld x", self.goodModel.addCartNum.integerValue / self.goodModel.minToBuy.intValue];
    }

    self.goodUnitLabel.text = [NSString stringWithFormat:@"%@/%@",self.goodModel.minToBuy, self.goodModel.unit];
    self.goodDescriptionWebView.scrollView.scrollEnabled = NO;
    self.goodDescriptionWebView.opaque = NO;
    NSString *content = self.goodModel.content != NULL ? self.goodModel.content : @"<p>暂无产品介绍</p>";
    [self.goodDescriptionWebView loadHTMLString:[NSString stringWithFormat:@"%@%@",imgStyle, content] baseURL:nil];
}

- (IBAction)addToCartAction:(UIButton *)sender {
//    [sender setEnabled:NO];
    if([EJSUserModel userModel]){
        [self updateAddedCartNum: self.goodModel.minToBuy.intValue];
        if(self.goodModel.hasDefaultSku.integerValue == 1 && self.goodModel.skus.count > 0){
            self.selectSkuModel = self.goodModel.skus.firstObject;
            self.selectSkuModel.skuSelectCount = self.goodModel.minToBuy.integerValue;
            [self addToCartReq:self.selectSkuModel];
            return;
        }
        FlyWeakSelf
        [EJSSelectGoodsSpecView showSelectGoodsSpecView:self.goodModel completion:^(EJSGoodsSkuModel *selectSkuModel) {
            [weakSelf addToCartReq:selectSkuModel];
        }];
    }
}

- (void)addToCartReq:(EJSGoodsSkuModel *)skuModel{
    EJSGoodsAddCartRequest *req = [EJSGoodsAddCartRequest new];
    if (skuModel.sku_id.length > 0) {
        req.skuId = skuModel.sku_id;
    }else {
        req.skuId = skuModel.skuId;
    }
    req.num = @(skuModel.skuSelectCount).stringValue;
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:@"加入购物车成功"];
        if(weakSelf.selectSkuModel){
            weakSelf.selectSkuModel = nil;
            [[XOShopCartViewModel shared]updateGoodsAddedCount];
        }
    } failHandler:^(BaseResponse *response) {
        [weakSelf updateAddedCartNum: -weakSelf.goodModel.minToBuy.intValue];
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)updateAddedCartNum: (int)count {
    self.goodModel.addCartNum = @(self.goodModel.addCartNum.intValue + count);
    self.goodAddedCountLabel.text = [NSString stringWithFormat:@"%d x", self.goodModel.addCartNum.intValue / self.goodModel.minToBuy.intValue];
}


@end


//"spuId" = ""
//"spuName" = ""
//"sn" = ""
//"cover" = ""
//"originPrice" ""
//"price" = ""
//"unit" = ""
//"minToBuy" = ""
//"content" = ""
//"status" = ""
//"hasDefaultSku" = ""
//"addCartNum" = 1
//"skus" =  [{
//    "skuId" = "",
//    "specInfo" = "规格",
//    "price" = ""
//},{
//     "skuId" = "",
//     "specInfo" = "规格",
//     "price" = ""
//           }]
//"specs" = [{
//    "specId" = "",
//    "specName" = "单位介绍",
//    "specs":[{
//        "mode" = "",
//        "value" = "规格单位"
//    }]
//}]
